const fs = require("fs");
const path = require("path");
const os = require("os");
const crypto = require("crypto");
class ConfigManager {
  constructor(_0x427d13 = null) {
    this.appRoot = _0x427d13;
    this.configPath = this.getConfigPath();
    this.ensureConfigDir();
    this.writeLock = Promise.resolve();
  }
  getConfigPath() {
    const _0x4d8b05 = os.platform();
    if (_0x4d8b05 === "darwin" && this.appRoot) {
      let _0x3a5a66 = path.join(this.appRoot, "resources", "app");
      if (!fs.existsSync(_0x3a5a66)) {
        _0x3a5a66 = this.appRoot;
      }
      const _0x232338 = path.join(_0x3a5a66, "extensions", "cursor-always-local", "dist");
      console.log("[ConfigManager] macOS配置文件路径:", path.join(_0x232338, "config.json"));
      return path.join(_0x232338, "config.json");
    } else if (_0x4d8b05 === "win32") {
      const _0x2c7763 = path.join(os.homedir(), "AppData", "Roaming", "CursorPro");
      return path.join(_0x2c7763, "config.json");
    } else {
      const _0x4e536f = path.join(os.homedir(), ".config", "CursorPro");
      return path.join(_0x4e536f, "config.json");
    }
  }
  ensureConfigDir() {
    const _0x145f81 = path.dirname(this.configPath);
    if (!fs.existsSync(_0x145f81)) {
      fs.mkdirSync(_0x145f81, {
        recursive: true
      });
    }
  }
  loadConfig() {
    const _0x5c8572 = {
      premium_usage: 0,
      max_premium_usage: 50,
      last_updated: "",
      uuid: ""
    };
    if (fs.existsSync(this.configPath)) {
      try {
        const _0xb2f518 = fs.readFileSync(this.configPath, "utf-8");
        const _0x28bedf = JSON.parse(_0xb2f518);
        const _0x185e03 = {
          ..._0x5c8572,
          ..._0x28bedf
        };
        const _0x5c4f2f = _0x185e03;
        return _0x5c4f2f;
      } catch (_0x5e2522) {
        return _0x5c8572;
      }
    }
    return _0x5c8572;
  }
  saveConfig(_0x361620) {
    this.writeLock = this.writeLock.then(async () => {
      try {
        const _0x3c0d7e = this.loadConfig();
        const _0x41e7e8 = {
          ..._0x3c0d7e,
          ..._0x361620
        };
        const _0x406619 = _0x41e7e8;
        const _0x80506f = this.configPath + ".tmp";
        const _0x2556d9 = JSON.stringify(_0x406619, null, 4);
        fs.writeFileSync(_0x80506f, _0x2556d9, "utf-8");
        fs.renameSync(_0x80506f, this.configPath);
        console.log("[ConfigManager] 配置保存成功:", Object.keys(_0x361620));
        return true;
      } catch (_0x9f4855) {
        console.error("[ConfigManager] 配置保存失败:", _0x9f4855);
        return false;
      }
    });
    return this.writeLock;
  }
  getOrCreateUUID() {
    const _0x57a699 = this.loadConfig();
    if (_0x57a699.uuid) {
      return _0x57a699.uuid;
    }
    const _0x4589ed = crypto.randomUUID();
    _0x57a699.uuid = _0x4589ed;
    if (this.saveConfig(_0x57a699)) {
      return _0x4589ed;
    }
    return _0x4589ed;
  }
  updateUUID(_0x3683c9) {
    if (!_0x3683c9) {
      return false;
    }
    const _0x19750e = this.loadConfig();
    if (_0x19750e.uuid === _0x3683c9) {
      return true;
    }
    _0x19750e.uuid = _0x3683c9;
    return this.saveConfig(_0x19750e);
  }
  getCurrentUUID() {
    const _0x50c9db = this.loadConfig();
    return _0x50c9db.uuid || "";
  }
  updateField(_0xe65c73, _0x174baa) {
    const _0x41243a = this.loadConfig();
    _0x41243a[_0xe65c73] = _0x174baa;
    return this.saveConfig(_0x41243a);
  }
  getField(_0x3481aa, _0x2e35ea = null) {
    const _0x4cd6ee = this.loadConfig();
    if (_0x4cd6ee[_0x3481aa] !== undefined) {
      return _0x4cd6ee[_0x3481aa];
    } else {
      return _0x2e35ea;
    }
  }
}
module.exports = ConfigManager;